<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
        <bro1></bro1>
        <bro2></bro2>
    </div>
    <!-- 兄弟一 -->
    <template id="bro1">
      <fieldset>
        <legend>兄弟一</legend>
        <input type="text"  v-model="val">
        <button @click="add">add</button>
      </fieldset>
    </template>
    <!-- 兄弟二 -->
    <template id="bro2">
      <fieldset>
        <legend>兄弟二</legend>
        <ul>
          <li v-for="(item,index) of arr" :key="index">{{item}}</li>
        </ul>
      </fieldset>
    </template>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    <script>
      //创建一个vue的实例
      let eventbus = new Vue({});
      Vue.component("bro1", {
        template: "#bro1",
        data() {
          return {
            val: "",
          };
        },
        methods: {
            add(){
                eventbus.$emit("fm98",this.val);
                this.val="";
            }
        }
      });
      Vue.component("bro2", {
        template: "#bro2",
        data() {
          return {
            arr: ["吃", "喝", "拉"],
          };
        },
        //钩子函数
        mounted() {
            //组件的钩子函数[小写]
            eventbus.$on("fm98",(input)=>{
                this.arr.push(input)
            })
        },
      });
      const vm = new Vue({
        el: "#app",
      });
    </script>
  </body>
</html>
